<template>
  <el-drawer v-model="drawerVisible" :destroy-on-close="true" size="450px" :title="`${drawerProps.title}角色`">
    <el-form
      ref="ruleFormRef"
      label-width="100px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="角色名称" prop="name">
        <el-input v-model="drawerProps.row!.name" placeholder="请填写角色名称" clearable></el-input>
      </el-form-item>
      <el-form-item label="权限字符" prop="roleKey">
        <el-input v-model="drawerProps.row!.roleKey" placeholder="请填写权限字符" clearable></el-input>
      </el-form-item>
      <el-form-item label="菜单权限"  prop="roles">
        <el-tree ref="tree" v-model="drawerProps.row!.roles" :data="routerMenu" :props="props" show-checkbox :check-on-click-node="true" node-key="id" />
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="drawerVisible = false"> 取消 </el-button>
      <el-button v-show="!drawerProps.isView" type="primary" @click="handleSubmit"> 确定 </el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="UserDrawer">
import { ref, reactive, onMounted } from "vue";
import { genderType } from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { User } from "@/api/interface";
import UploadImg from "@/components/Upload/Img.vue";
import UploadImgs from "@/components/Upload/Imgs.vue";
import { getMenuListApi } from "@/api/modules/login";
import { getCurrentInstance } from "vue";
const { ctx } = getCurrentInstance();

const rules = reactive({
  name: [{ required: true, message: "请填写角色名称" }],
  roleKey: [{ required: true, message: "请填写权限字符" }],
 /*  roles: [{ required: true, message: "请选择菜单权限",trigger: "change"}], */
});
const routerMenu = ref<any>([]);

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<User.ResUserList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});
const props = {
  label: (t: any) => t.meta.title,
  children: "children"
};

onMounted(async () => {
  let result: any = await getMenuListApi();
  routerMenu.value = result.data.routerMenu;
});

// 接收父组件传过来的参数
const acceptParams = (params: any) => {
  drawerProps.value = params;
    const rolesRes = params.row.roles?.split(",");
    ctx.$nextTick(() => {
      ctx.$refs.tree.setCheckedKeys([]);
      rolesRes?.map((v: any) => {
        ctx.$refs.tree.setChecked(v, true, false);
      });
    });
  
  drawerVisible.value = true;
};/* 就比如我这个函数，接受参数params,如果params中的row没有roles是不是会报错？？？用?.就可以让roles不存在的时候表达式直接短路，
不执行 */

// 提交数据（新增/编辑）
const ruleFormRef = ref<FormInstance>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      let roles = ctx.$refs.tree.getCheckedNodes(false, true).map((t: any) => t.id);
      await drawerProps.value.api!({ ...drawerProps.value.row, roles: roles.toString() });
      ElMessage.success({ message: `${drawerProps.value.title}角色成功！` });
      drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>
